/**
 * Copyright (c) 2011-2014, homeant (homeanter@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package xin.homeant.upms.client.shiro.filter;


import java.net.URLEncoder;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.AuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;

import xin.homeant.common.utils.HttpUtils;





/**
 * <P>
 * authc 过滤器
 * </P>
 * 
 * @author homeant homeanter@163.com
 * @Data 2017年5月21日 下午4:01:33
 */
public class UpmsAuthenticationFilter extends AuthenticationFilter{
	
	/**
	 * 认证通过
	 */
	@Override
	protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
		Subject subject = getSubject(request, response);
        return subject.isAuthenticated();
	}
	
	/**
	 * 认证失败
	 */
	@Override
	protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
		HttpServletRequest servletRequest = (HttpServletRequest) request;
		String backUrl = HttpUtils.getRequestUrl(servletRequest);
		WebUtils.toHttp(response).sendRedirect(getLoginUrl()+"?redirect_uri="+URLEncoder.encode(backUrl, "UTF-8"));
        return false;
	}
	

}
